<?php
/**
 *
 *
 * PHP version 5.3.x
 *
 * @category
 * @package
 * @author   gouki <gouki.xiao@gmail.com>
 */
/**
 * SaeAssetManager.php
 *
 * @category
 * @package
 * @author   gouki <gouki.xiao@gmail.com>
 * @version $Id$
 * @created 2011-11-18 16:45
 */
class SaeAssetManager extends CAssetManager
{
	/**
	 * @var string base web accessible path for storing private files
	 */
	private $_basePath;
	/**
	 * @var string base URL for accessing the publishing directory.
	 */
	private $_baseUrl;
	/**
	 * @var array published assets
	 */
	private $_published=array();

    public function setBasePath($value){
        if(basename($value) == $this->getPublicParams()){
            $this->_basePath = $this->getPublicParams();
        }
    }
    /**
   	 * Publishes a file or a directory.
   	 * This method will copy the specified asset to a web accessible directory
   	 * and return the URL for accessing the published asset.
   	 * <ul>
   	 * <li>If the asset is a file, its file modification time will be checked
   	 * to avoid unnecessary file copying;</li>
   	 * <li>If the asset is a directory, all files and subdirectories under it will
   	 * be published recursively. Note, in this case the method only checks the
   	 * existence of the target directory to avoid repetitive copying.</li>
   	 * </ul>
   	 * @param string $path the asset (file or directory) to be published
   	 * @param boolean $hashByName whether the published directory should be named as the hashed basename.
   	 * If false, the name will be the hashed dirname of the path being published.
   	 * Defaults to false. Set true if the path being published is shared among
   	 * different extensions.
   	 * @param integer $level level of recursive copying when the asset is a directory.
   	 * Level -1 means publishing all subdirectories and files;
   	 * Level 0 means publishing only the files DIRECTLY under the directory;
   	 * level N means copying those directories that are within N levels.
   	 * @param boolean $forceCopy whether we should copy the asset file or directory even if it is already published before.
   	 * This parameter is set true mainly during development stage when the original
   	 * assets are being constantly changed. The consequence is that the performance
   	 * is degraded, which is not a concern during development, however.
   	 * This parameter has been available since version 1.1.2.
   	 * @return string an absolute URL to the published asset
   	 * @throws CException if the asset to be published does not exist.
   	 */
    public function publish($path, $hashByName = false, $level = -1, $forceCopy = false) {
        $saeStorage = new SaeStorage();
        unset($this->_published[$path]);
        if (isset($this->_published[$path])) {
            return $this->_published[$path];
        } elseif (($src = realpath($path)) !== false) {
            if (is_file($src)) {
                $dir = $this->hash($hashByName ? basename($src) : dirname($src));
                $fileName = basename($src);
                echo $dir."/".$fileName;
                if(!$saeStorage->fileExists($this->getPublicParams(),$dir."/".$fileName)){
                    $saeStorage->write($this->getPublicParams(),$dir."/".$fileName,file_get_contents($src));
                }
                return $this->_published[$path] = $this->getBaseUrl() .'/' . "/$dir/$fileName";

            } else if (is_dir($src)) {
                $dir = $this->hash($hashByName ? basename($src) : $src);
                if(!$saeStorage->fileExists($this->getPublicParams(),$dir."/index.html")){
                    $saeStorage->write($this->getPublicParams(),$dir."/index.html","");
                }
                SaeFileHelper::copyDirectory( $this->getPublicParams(),$src,$dir,array(
						'exclude'=>$this->excludeFiles,
						'level'=>$level,
						'newDirMode'=>$this->newDirMode,
						'newFileMode'=>$this->newFileMode,
			    ));
                return $this->_published[$path] = $this->getBaseUrl() . '/'. $dir;
            }
        }
        throw new CException(Yii::t('yii', 'The asset "{asset}" to be published does not exist.', array('{asset}' => $path)));
    }

    private function getPublicParams(){
        $assets = SaeCommon::getSaeStorage('assets');
        if($assets){
            return $assets;
        }
        throw new CException(Yii::t("yiisae","Please set the assets domain for sae, and set Yii::app()->params['storage_domain']['assets']"));
        //return "assets";
    }
}
